<?php

namespace App\Model;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class RoomMonthWater extends BaseModel
{
    protected $connection = 'mysql';
    protected $table = 'finance_room_wealth_month';
    public $timestamps = false;


    public function getList($request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $month_date = $request->input('month', '');
        $query = new self();
        if ($month_date) $query = $query->where('month', $month_date);
        return $query->orderBy('id', 'DESC')->paginate($limit, ['*'], 'page', $page);
    }

    public function getRoomMonthSumWater($request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $month_date = $request->input('begin_date');
        $where = [];
        if($month_date){
            $where['month'] = date('Ym',strtotime($month_date));;
        }
        $data = DB::connection('api')->table($this->table)
            ->select('month',
                DB::raw('MAX(type) as type'),
                DB::raw('MAX(default_ratio) as default_ratio'),
                DB::raw('SUM(wealth) as sum_wealth'),
                DB::raw('SUM(original) as sum_original'),
                DB::raw('SUM(modify) as sum_modify'))
            ->where($where)
            ->groupBy('month')
            ->orderBy('month','desc')
            ->get();
        foreach($data as &$v){
            $v->sum_original = $v->sum_original / 100;
            $v->sum_modify = $v->sum_modify / 100;
        }
        $total = count($data);
        $res['data'] = collect($data)->forPage($page, $limit)->values()->toArray();
        $res['total'] = $total;
        return $res;
    }

    public function getAllRoomWaterList($request)
    {
        $limit = $request->input('limit', 10);
        $page = $request->input('page', 1);
        $where['month'] = $request->input('month');
        if($request->input('room_uid')){
            $where['room_uid'] = $request->input('room_uid');
        }
        if($request->input('uuid')){
            $where['uuid'] = $request->input('uuid');
        }
        $data =  DB::connection('api')->table('finance_room_wealth_month as f')
            ->join('room', 'room.room_id', '=', 'f.room_id')
            ->join('user', 'user.user_id', '=', 'f.user_id')
            ->select('f.*','room.room_uid','uuid')
            ->where($where)->get();
        foreach($data as &$v){
            $v->original = $v->original / 100;
            $v->modify = $v->modify / 100;
            $v->before_total = $v->before_total / 100;
        }
        $total = count($data);
        $res['data'] = collect($data)->forPage($page, $limit)->values()->toArray();
        $res['total'] = $total;
        return $res;
    }

    public function checkRoomWaterList($where)
    {

        $res =  DB::connection('api')->table('finance_room_wealth_month as f')
            ->where($where)->first();
        return $res;
    }


    /*
        * 添加
        * */
    public function createData($data)
    {
        return  curl_request(getKkaudioApiUrl() . '/inner/finance/room/wealth_month', $data , 'urlencoded', 'GET');
    }

}
